﻿using System.Configuration;

namespace K3R.Web.Interface {
	public class ConfigProfileElement : ConfigurationElement {
		[ConfigurationProperty("code", IsRequired = true, IsKey=true)]
		public string Code {
			get {
				return (string)base["code"];
			}
			set {
				base["code"] = value;
			}
		}
		[ConfigurationProperty("name", IsRequired = true)]
		public string Name {
			get {
				return (string)base["name"];
			}
			set {
				base["name"] = value;
			}
		}

		[ConfigurationProperty("sqlLogging", DefaultValue = false)]
		public bool SqlLogging {
			get {
				return (bool)base["sqlLogging"];
			}
			set {
				base["sqlLogging"] = value;
			}
		}

		[ConfigurationProperty("connectionStringName", IsRequired = true)]
		public string ConnectionStringName {
			get {
				return (string)base["connectionStringName"];
			}
			set {
				base["connectionStringName"] = value;
			}
		}
	}

	public class ConfigProfileCollection : ConfigurationElementCollection {
		protected override ConfigurationElement CreateNewElement() {
			return new ConfigProfileElement();
		}

		protected override object GetElementKey(ConfigurationElement element) {
			return (element as ConfigProfileElement).Code;
		}

		public ConfigProfileElement this[int index] {
			get {
				return (ConfigProfileElement)BaseGet(index);
			}
			set {
				if (BaseGet(index) != null) {
					BaseRemoveAt(index);
				}
				BaseAdd(index, value);
			}
		}

		public new ConfigProfileElement this[string code] {
			get {
				return (ConfigProfileElement)BaseGet(code);
			}
		}

		public int IndexOf(ConfigProfileElement profile) {
			return BaseIndexOf(profile);
		}

		public new int Count {
			get { return base.Count; }
		}
	}

	public class InterfaceConfig : ConfigurationSection {
		[ConfigurationProperty("profiles", IsDefaultCollection= true)]
		[ConfigurationCollection(typeof(ConfigProfileCollection), AddItemName="profile")]
		public ConfigProfileCollection Profiles {
			get {
				return (ConfigProfileCollection)base["profiles"];
			}
			set {
				base["profiles"] = value;
			}
		}
	}
}
